package com.exam.master.model.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.exam.commonbiz.base.BaseActivity;
import com.exam.commonbiz.bean.UserInfo;
import com.exam.commonbiz.model.ILoginCallBack;
import com.exam.commonbiz.util.OnFastClickListener;
import com.exam.commonbiz.util.ToastUtil;
import com.exam.commonbiz.viewmodel.CommonViewModel;
import com.exam.master.R;
import com.exam.master.databinding.ActivityLoginBinding;
import com.exam.master.model.main.view.MainActivity;

public class LoginActivity extends BaseActivity<CommonViewModel, ActivityLoginBinding> implements ILoginCallBack {

    public static void start(Context context) {
        Intent starter = new Intent(context, LoginActivity.class);
        context.startActivity(starter);
        ((Activity)context).finish();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initData() {

        mViewModel.setLoginCallBack(this);
        binding.btnSubmit.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                String phone = binding.edtPhone.getText().toString();
                String psd = binding.edtPsd.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showLongToastCenter("手机号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(psd)) {
                    ToastUtil.showLongToastCenter("密码不能为空");
                    return;
                }
                UserInfo userInfo = new UserInfo();
                userInfo.phone = phone;
                userInfo.password = psd;
                mViewModel.login(userInfo);
            }
        });

        binding.tvToRegister.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                RegisterActivity.start(context);
            }
        });
    }

    @Override
    public int getStatusBarColor() {
        return R.color.color_bg;
    }

    @Override
    public void onLoginSuccess() {
        ToastUtil.showLongToastCenter("登录成功");
        MainActivity.start(context);
        finish();
    }

    @Override
    public void onLoginFailed() {

    }
}